package cn.itcast.web.controller.cargo;

import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping(value = "/cargo/contract",name ="购销合同管理")
public class ContractController extends BaseController {
    @Reference
    private ContractService contractService;

    @RequestMapping(value = "/list",name = "合同分页查询")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "3") Integer pageSize){
        //example类用于添加条件，相当where后面的部分,用于编写拼接dao层没有的sql语句
        ContractExample contractExample = new ContractExample();
        //条件对象
        ContractExample.Criteria criteria = contractExample.createCriteria();
        //指定企业Id条件
        criteria.andCompanyIdEqualTo(getCompanyId());
        //时间降序
        contractExample.setOrderByClause("create_time desc");
        //细颗粒度权限管理(面试题有相关解释)
        User loginUser = getLoginUser();
        if (loginUser.getDegree() == 2){
               //2：管理下级部门（北京总部经理）
              //3：管理本部门（顺义校区经理）
             //4：普通员工（顺义校区普通员工）
        }else if (loginUser.getDegree() == 3){
            criteria.andCreateDeptEqualTo(loginUser.getDeptId());
        }else if (loginUser.getDegree() == 4){
            criteria.andCreateByEqualTo(loginUser.getId());
        }

        PageInfo page = contractService.findByPage(pageNum, pageSize,contractExample);
        request.setAttribute("page",page);


        return "cargo/contract/contract-list";
    }


    @RequestMapping(value = "/toAdd", name = "新增页面")
    public String toAdd() {
        return "cargo/contract/contract-add";
    }
    @RequestMapping(value = "/edit", name = "保存或修改")
    public String edit(Contract contract) {
        if(StrUtil.hasEmpty(contract.getId())){
        contract.setCompanyId(getCompanyId());
        contract.setCreateBy(getLoginUser().getId());
        contract.setCreateDept(getLoginUser().getDeptId());
        contract.setCreateTime(new Date());
        contractService.save(contract);
    }else{
            // 指定修改人id、修改时间
            contract.setUpdateBy(getLoginUser().getUserName());
            contract.setUpdateTime(new Date());
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){

        Contract contract = contractService.findById(id);
        request.setAttribute("contract", contract);
        return "cargo/contract/contract-update";
    }


    @RequestMapping(value = "/delete", name = "删除")
    public String delete(String id) {
        contractService.delete(id);
        return "redirect:/cargo/contract/list.do";
    }


    @RequestMapping(value = "/submit",name = "提交")
    public String submit(String id){
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(1);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }

    @RequestMapping(value = "/cancel",name = "取消")
    public String cancel(String id){
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(0);
        contractService.update(contract);
        return "redirect:/cargo/contract/list.do";
    }
}
